package com.common.manager;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.common.model.OrgInfoModel;
import com.common.model.RoleOrgModel;
import com.common.model.StaffInfoModel;
import com.common.utils.ASObjectConvert;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;

import flex.messaging.io.amf.ASObject;



@Component
public class OrgInfoManager  extends BaseManager{
	
	
	@Override
	public void setSqlMapNameSpace() {
		this.sqlMapNameSpace = "com.common.security.model.OrgInfo";
	}
	
	/**
	 * 功能：检查机构编码是否正常
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public boolean checkOrgCode(String orgCode) throws Exception {
		
		IData conditionObj = new DataMap();
		conditionObj.put("orgCode", orgCode);
		OrgInfoModel orgInfoModel =(OrgInfoModel)super.getObjectInfo("checkObjectInfo",conditionObj);
		if(orgInfoModel!=null){
			return false;
		}
		return true;
	}
	
	
	public void delOrg(ASObject object) {
		IData idata = ASObjectConvert.convertASObject2JavaObject(object,DataMap.class);
		super.deleteObject(idata);
	}
	
	
	/**
	 * 功能：加载机构树列表
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public String loadOrgTree(StaffInfoModel staffInfoModel,String menuUrl,String isFlag) throws Exception {
		IData idata = new DataMap();
		idata.put("orgCode", staffInfoModel.getOrgCode());
		idata.put("roleIds", staffInfoModel.getRoleids());
		List<OrgInfoModel> orgist = super.getObjectList("getTreeList",idata);
		JSONArray orgList = new JSONArray();
		JSONObject orgObj;
		if(orgist!=null){
			for (OrgInfoModel object : orgist) {
				 orgObj = new JSONObject();
				orgObj.put("id", object.getOrgCode());
				orgObj.put("pId", object.getPorgCode());
				orgObj.put("name", object.getOrgName());
				orgObj.put("open","true");
				if(isFlag.equals("STAFF")){
					orgObj.put("url",menuUrl+"?orgCode="+object.getOrgCode()+"&orgName="+object.getOrgName());
				}else{
					orgObj.put("url",menuUrl+"?porgCode="+object.getOrgCode());
				}
				orgObj.put("tabid","taborg");
				orgObj.put("fresh","true");
				orgList.add(orgObj);
			}
		}else{
			 orgObj = new JSONObject();
				orgObj.put("id", "-1");
				orgObj.put("pId", "-1");
				orgObj.put("name", "机构树初始化");
				orgObj.put("open","true");
				orgObj.put("url",menuUrl+"?porgCode=-1");
				orgObj.put("tabid","taborg");
				orgObj.put("fresh","true");
				orgList.add(orgObj);
		}
	  return orgList.toString();
	}
	
	
	/**
	 * 功能：加载机构人员树列表
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public String loadOrgStaffTree(StaffInfoModel staffInfoModel,String menuUrl,String isFlag) throws Exception {
		IData idata = new DataMap();
		idata.put("orgCode", staffInfoModel.getOrgCode());
		idata.put("roleIds", staffInfoModel.getRoleids());
		List<OrgInfoModel> orgist = super.getObjectList("getTreeList",idata);
		JSONArray orgList = new JSONArray();
		JSONObject orgObj;
		if(orgist!=null){
			for (OrgInfoModel object : orgist) {
				 orgObj = new JSONObject();
				orgObj.put("id", object.getOrgCode());
				orgObj.put("pId", object.getPorgCode());
				orgObj.put("name", object.getOrgName());
				orgObj.put("open","true");
				if(isFlag.equals("STAFF")){
					orgObj.put("url",menuUrl+"?orgCode="+object.getOrgCode()+"&orgName="+object.getOrgName());
				}else{
					orgObj.put("url",menuUrl+"?porgCode="+object.getOrgCode());
				}
				orgObj.put("tabid","taborg");
				orgObj.put("fresh","true");
				orgList.add(orgObj);
			}
		}else{
			 orgObj = new JSONObject();
				orgObj.put("id", "-1");
				orgObj.put("pId", "-1");
				orgObj.put("name", "机构名称");
				orgObj.put("open","true");
				orgObj.put("url",menuUrl+"?porgCode=-1");
				orgObj.put("tabid","taborg");
				orgObj.put("fresh","true");
				orgList.add(orgObj);
		}
	  return orgList.toString();
	}
	
	/**
	 * 读取角色授权数据权限机构树
	 * @param roleId
	 * @return
	 */
	public String getRoleOrgTreeList(String roleId){
		IData idata = new DataMap();
		idata.put("roleId", roleId);
		JSONArray treeList = new JSONArray();
		List<RoleOrgModel> objList = super.getObjectList("getRoleOrgTreeList",idata);
		if(objList!=null){
			JSONObject orgObj = new JSONObject();
			for (int i = 0; i < objList.size(); i++) {
				RoleOrgModel roleOrgModel =  (RoleOrgModel) objList.get(i);
				orgObj = new JSONObject();
				orgObj.put("id", roleOrgModel.getId());
				orgObj.put("pId", roleOrgModel.getPId());
				orgObj.put("name", roleOrgModel.getLabel());
				if("-1".equals(roleOrgModel.getPId())){
					orgObj.put("open","true");
				}
				if(roleOrgModel.getSelFlag()!=null){
					orgObj.put("checked", true);
				}
				orgObj.put("intID", roleOrgModel.getIntID());
				treeList.add(orgObj);
			}
		}
	  log.info(treeList.toString());
	  return treeList.toString();
	}
}
